package com.example.liunanxin.lnx226ordering.Fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.liunanxin.lnx226ordering.R;
import com.example.liunanxin.lnx226ordering.activities.ModifyActivity;
import com.example.liunanxin.lnx226ordering.activities.OrderListActivity;
import com.example.liunanxin.lnx226ordering.activities.SettingActivity;
import com.example.liunanxin.lnx226ordering.beans.UserinfoBean;
import com.example.liunanxin.lnx226ordering.iface.UserinfoListener;
import com.example.liunanxin.lnx226ordering.model.UserinfoModel;

public class UserFragment extends Fragment {

    private UserinfoBean userinfoBeans;
    private TextView tv_username,tv_phone,tv_shopping,tv_order,tv_comment;
    private ImageView iv_setting,iv_more;
    private SharedPreferences sharedPreferences;
    private int user_id;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_me,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initView(view);

        sharedPreferences=getActivity().getSharedPreferences("user",Context.MODE_PRIVATE);
        user_id=Integer.parseInt(sharedPreferences.getString("userid","获取userid失败"));

        UserinfoModel userinfoModel=new UserinfoModel();
        userinfoModel.getUserById(user_id,userinfoListener);

        click();
    }

    public void initView(View view){
        tv_username=view.findViewById(R.id.textView26);
        tv_phone=view.findViewById(R.id.textView27);
        tv_shopping=view.findViewById(R.id.textView28);
        tv_order=view.findViewById(R.id.textView29);
        tv_comment=view.findViewById(R.id.textView30);
        iv_setting=view.findViewById(R.id.imageView24);
        iv_more=view.findViewById(R.id.imageView25);
    }


    private void click(){
        iv_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(),ModifyActivity.class);
                intent.putExtra("user_id",user_id);
                intent.putExtra("username",userinfoBeans.getUsername());
                intent.putExtra("phone",userinfoBeans.getMobilenum());
                intent.putExtra("address",userinfoBeans.getAddress());
                startActivity(intent);
            }
        });

        iv_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(),SettingActivity.class);
                startActivity(intent);
            }
        });

        tv_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(),OrderListActivity.class);
                startActivity(intent);
            }
        });
    }


    UserinfoListener userinfoListener=new UserinfoListener() {
        @Override
        public void onResponse(UserinfoBean userinfoBean) {
            userinfoBeans=userinfoBean;
            tv_username.setText(userinfoBean.getUsername());
            tv_phone.setText(userinfoBean.getMobilenum());
        }

        @Override
        public void onFail(String mag) {
            Toast.makeText(getContext(),mag,Toast.LENGTH_SHORT).show();
        }
    };

}
